package cl.alto.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cl.alto.clases.ObjetoPropio;

public class DaoStandard {


public static List<ObjetoPropio> get( long valor) throws SQLException, Exception{
        Connection cnx = null;
        Statement csmt  = null;
        ResultSet rs = null;
        List<ObjetoPropio> list = new ArrayList<ObjetoPropio>();        
        try {
        	cnx = ConexionLoca.getConnection();

            String sql = "select * from Isapre where 555 = "+valor;
            csmt = cnx.createStatement();
            rs = csmt.executeQuery(sql);
            while(rs.next()){
                ObjetoPropio bean = new ObjetoPropio();
                String campo1 = rs.getString("nombre");
                String campo2 = rs.getString("nombre");
                
                bean.setCampo1(campo1);
                bean.setCampo2(campo2);
                list.add(bean);
            }
            return list;
        } catch (SQLException e) {
            //e.printStackTrace();
            throw e;
        } catch (Exception e) {
            //e.printStackTrace();
            throw e;
        } finally {
            destroy(cnx,csmt,rs);
        }
    }
    
    public int delete( long valor) throws SQLException, Exception {
        Connection cnx = null;
        Statement csmt = null;
        try {
            cnx = ConexionLoca.getConnection();
            String sql = "delete from tabla where campo1 = valor";
            csmt = cnx.createStatement();
            return csmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            destroy(cnx,csmt,null);
        }
    }
    

    public int insert(ObjetoPropio bean) throws SQLException, Exception {
        Connection cnx = null;
        Statement csmt = null;
        ResultSet rs = null;
        try {
//            cnx = Coneccion;//CONECCION!
            String insert = "insert into TABLA (";
            insert +="campo1,";
            insert +="campo2";
            insert +=")VALUES(";
//            insert +="'" + bean.getCampo1() +"',";
//            insert +="'" + bean.getCampo2() +"'";
            insert +=")";
            csmt = cnx.createStatement();
            return csmt.executeUpdate(insert) ;
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            destroy(cnx,csmt,rs);
        }
    }
    
    public int update(ObjetoPropio bean) throws SQLException, Exception {
        Connection cnx = null;
        Statement csmt = null;
        ResultSet rs = null;
        try {
//            cnx = Coneccion;//CONECCION!
            String insert = "update TABLA set";
            insert +="campo1 =";
            insert +="'" + bean.getCampo1() +"',";
            insert +="campo2=";
            insert +="'" + bean.getCampo2() +"'";
            insert +="WHERE CAMPO=VALOR";
            csmt = cnx.createStatement();
            return csmt.executeUpdate(insert) ;
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            destroy(cnx,csmt,rs);
        }
    }
    
    /*ATOMIZABLE Y REUTIZABLE*/
    
    public static void destroy(Connection con, Statement stmt, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (final SQLException ee) {
            rs = null;
        }
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (final SQLException ee) {
            stmt = null;
        }
        try {
            if (con != null) {
                con.close();
            }
        } catch (final SQLException ee) {
            con = null;
        }
    }
}
